import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { SERVER_API_URL } from '../../app.constants';

import { JhiDateUtils } from 'ng-jhipster';

import { ShoppingCart } from './shopping-cart.model';
import { createRequestOption } from '../../shared';
import {BookBasic} from '../book-basic';

export type EntityResponseType = HttpResponse<ShoppingCart>;

@Injectable()
export class ShoppingCartService {

    private resourceUrl =  SERVER_API_URL + 'api/shopping-carts';

    constructor(private http: HttpClient, private dateUtils: JhiDateUtils) { }

    addShoppingCart(book: BookBasic, account: Account, amount: Number) : Observable<EntityResponseType> {
        const shoppingCart: ShoppingCart = new ShoppingCart();
        shoppingCart.bookId = book;
        shoppingCart.jhiNumber = <number>amount;
        shoppingCart.bookPrice = <number> amount * book.price;
        shoppingCart.userId = parseInt (account.id);

        const copy = this.convert(shoppingCart);
        return this.http.post<ShoppingCart>(this.resourceUrl, copy, { observe: 'response' })
            .map((res: EntityResponseType) => this.convertResponse(res));
    }

    create(shoppingCart: ShoppingCart): Observable<EntityResponseType> {
        const copy = this.convert(shoppingCart);
        return this.http.post<ShoppingCart>(this.resourceUrl, copy, { observe: 'response' })
            .map((res: EntityResponseType) => this.convertResponse(res));
    }

    update(shoppingCart: ShoppingCart): Observable<EntityResponseType> {
        const copy = this.convert(shoppingCart);
        return this.http.put<ShoppingCart>(this.resourceUrl, copy, { observe: 'response' })
            .map((res: EntityResponseType) => this.convertResponse(res));
    }

    find(id: number): Observable<EntityResponseType> {
        return this.http.get<ShoppingCart>(`${this.resourceUrl}/${id}`, { observe: 'response'})
            .map((res: EntityResponseType) => this.convertResponse(res));
    }

    query(req?: any): Observable<HttpResponse<ShoppingCart[]>> {
        const options = createRequestOption(req);
        return this.http.get<ShoppingCart[]>(this.resourceUrl, { params: options, observe: 'response' })
            .map((res: HttpResponse<ShoppingCart[]>) => this.convertArrayResponse(res));
    }

    delete(id: number): Observable<HttpResponse<any>> {
        return this.http.delete<any>(`${this.resourceUrl}/${id}`, { observe: 'response'});
    }

    private convertResponse(res: EntityResponseType): EntityResponseType {
        const body: ShoppingCart = this.convertItemFromServer(res.body);
        return res.clone({body});
    }

    private convertArrayResponse(res: HttpResponse<ShoppingCart[]>): HttpResponse<ShoppingCart[]> {
        const jsonResponse: ShoppingCart[] = res.body;
        const body: ShoppingCart[] = [];
        for (let i = 0; i < jsonResponse.length; i++) {
            body.push(this.convertItemFromServer(jsonResponse[i]));
        }
        return res.clone({body});
    }

    /**
     * Convert a returned JSON object to ShoppingCart.
     */
    private convertItemFromServer(shoppingCart: ShoppingCart): ShoppingCart {
        const copy: ShoppingCart = Object.assign({}, shoppingCart);
        copy.orderTime = this.dateUtils
            .convertDateTimeFromServer(shoppingCart.orderTime);
        return copy;
    }

    /**
     * Convert a ShoppingCart to a JSON which can be sent to the server.
     */
    private convert(shoppingCart: ShoppingCart): ShoppingCart {
        const copy: ShoppingCart = Object.assign({}, shoppingCart);

        copy.orderTime = this.dateUtils.toDate(shoppingCart.orderTime);
        return copy;
    }
    public postshopcartamount = (shoppingCart: ShoppingCart ): Observable<any> => {
        return this.http.post<any>(this.resourceUrl ,shoppingCart );
    }

    public deleteByuser= (): Observable<any> => {
        return this.http.delete(this.resourceUrl+'/user');
    }
}
